package chaowan.Mapper;
import chaowan.pojo.UserCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserCartMapper {

    @Select("SELECT * FROM user_cart")
    List<UserCart> selectAll();

    @Select("SELECT * FROM user_cart WHERE cart_id = #{cartId}")
    UserCart selectById(Long cartId);

    @Insert("INSERT INTO user_cart(user_id, goods_id, count, checked) " +
            "VALUES(#{userId}, #{goodsId}, #{count}, #{checked})")
    @Options(useGeneratedKeys = true, keyProperty = "cartId")
    int insert(UserCart userCart);

    @Update("UPDATE user_cart SET user_id=#{userId}, goods_id=#{goodsId}, count=#{count}, checked=#{checked} WHERE cart_id=#{cartId}")
    int update(UserCart userCart);

    @Delete("DELETE FROM user_cart WHERE cart_id = #{cartId}")
    int deleteById(Long cartId);

    @Select("SELECT * FROM user_cart WHERE user_id = #{uid} AND goods_id = #{gid}")
    UserCart findByUserAndGoods(@Param("uid") Long uid, @Param("gid") Long gid);

    @Insert("INSERT INTO user_cart (user_id, goods_id, count, checked) VALUES (#{userId}, #{goodsId}, #{count}, #{checked})")
    void save(UserCart cart);

    @Select("SELECT * FROM user_cart WHERE user_id = #{userId}")
    List<UserCart> selectByUserId(Long userId);

    @Select({
            "<script>",
            "SELECT * FROM user_cart WHERE cart_id IN",
            "<foreach collection='cartIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<UserCart> selectBatchIds(@Param("cartIds") List<Long> cartIds);

    @Delete({
            "<script>",
            "DELETE FROM user_cart WHERE cart_id IN",
            "<foreach collection='cartIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    void deleteBatchIds(@Param("cartIds") List<Long> cartIds);
}